home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / Blitter / Rabbit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-15  |  5.1 KB  |  207 lines

  1. /* Dice: 1> dcc -l0 -mD dpk.o tags.o Rabbit.c -o Rabbit
  2. **
  3. ** Blits a Rabbit to screen using a bob structure.  Control using
  4. ** the joystick.
  5. */
  6.  
  7. #include <proto/dpkernel.h>
  8.  
  9. BYTE *ProgName      = "Rabbit";
  10. BYTE *ProgAuthor    = "Paul Manias";
  11. BYTE *ProgDate      = "August 1998";
  12. BYTE *ProgCopyright = "DreamWorld Productions (c) 1998.  Freely distributable.";
  13. BYTE *ProgShort     = "Bob Demonstration.";
  14.  
  15. struct GScreen  *screen;
  16. struct Restore  *restore;
  17. struct Bob      *Rabbit;
  18. struct JoyData  *joydata;
  19. struct Picture  *bobpic;
  20. struct FileName bobfile  = { ID_FILENAME, "GMS:demos/data/Rabbit.iff" };
  21.  
  22. WORD RabbitFrames[] = {
  23.    /* Right 0 - 7 */
  24.    0,0, 8,0, 16,0, 24,0, 32,0, 40,0, 48,0, 56,0,
  25.  
  26.    /* Left 8 - 15 */
  27.    120,0, 112,0, 104,0, 96,0, 88,0, 80,0, 72,0, 64,0,
  28.  
  29.    /* Jumping 16 - 24 */
  30.    128,0, 136,0, 144,0, 152,0, 160,0, 168,0, 176,0, 184,0, 192,0,
  31.  
  32.    /* Backward Flip 25 - 47 */
  33.    0,16,     8,16,  16,16,  24,16,  32,16,  40,16,  48,16,  56,16,
  34.    64,16,   72,16,  80,16,  88,16,  96,16, 104,16, 112,16, 120,16,
  35.    128,16, 136,16, 144,16, 152,16, 160,16, 168,16, 176,16,
  36.    -1,-1
  37. };
  38.  
  39. void Demo(void);
  40. void Wrap(struct Bob *);
  41.  
  42. /***************************************************************************/
  43.  
  44. void main(void) {
  45.   LONG *palette;
  46.  
  47.   if (bobpic = Get(ID_PICTURE)) {
  48.      bobpic->Source = &bobfile;
  49.      bobpic->Bitmap->MemType = MEM_BLIT;
  50.  
  51.    if (Init(bobpic,NULL)) {
  52.  
  53.     if (screen = Get(ID_SCREEN)) {
  54.      if (palette = CloneMemBlock(bobpic->Bitmap->Palette,MEM_DATA)) {
  55.         screen->Bitmap->Palette = palette;
  56.  
  57.       if (Init(screen,NULL)) {
  58.  
  59.        if (restore = InitTags(screen,
  60.             TAGS_RESTORE, NULL,
  61.             RSA_Entries,  1,
  62.             TAGEND)) {
  63.  
  64.         if (Rabbit = InitTags(screen,
  65.             TAGS_BOB,      NULL,
  66.             BBA_GfxCoords, RabbitFrames,
  67.             BBA_Width,     8,
  68.             BBA_Height,    16,
  69.             BBA_XCoord,    screen->Width/2,
  70.             BBA_YCoord,    screen->Height/2,
  71.             BBA_Attrib,    BBF_RESTORE|BBF_GENMASKS|BBF_CLIP,
  72.             BBA_SrcBitmap, bobpic->Bitmap,
  73.             TAGEND)) {
  74.  
  75.          if (joydata = Get(ID_JOYDATA)) {
  76.             joydata->Port = 2;     /* Forces joystick control */
  77.  
  78.           if (Init(joydata, NULL)) {
  79.              Display(screen); 
  80.              Demo();
  81.           }
  82.          }
  83.         }
  84.        }
  85.       }
  86.      }
  87.     }
  88.    }
  89.   }
  90.  
  91.   FreeMemBlock(palette);
  92.   Free(joydata);
  93.   Free(Rabbit);
  94.   Free(restore);
  95.   Free(screen);
  96.   Free(bobpic);
  97. }
  98.  
  99. /***************************************************************************/
  100.  
  101. void Demo(void)
  102. {
  103.   WORD AnimSpeed = 0;
  104.   WORD Direction = 0;
  105.   WORD Flip = FALSE;
  106.  
  107.   #define MAXSPEED 3
  108.  
  109.   do
  110.   {
  111.     Activate(restore); 
  112.     Draw(Rabbit);
  113.     WaitAVBL();
  114.     SwapBuffers(screen);
  115.  
  116.     /* Animate the Rabbit's movements */
  117.  
  118.     AnimSpeed++;
  119.  
  120.     if (AnimSpeed > MAXSPEED) {
  121.        AnimSpeed = 0;
  122.        Rabbit->Frame++;
  123.  
  124.        if (Direction IS 1) {
  125.           if (Rabbit->Frame < 0) Rabbit->Frame = 0;
  126.           if (Rabbit->Frame > 7) Rabbit->Frame = 0;
  127.        }
  128.        else if (Direction IS -1) {
  129.           if (Rabbit->Frame < 8) Rabbit->Frame  = 8;
  130.           if (Rabbit->Frame > 15) Rabbit->Frame = 8;
  131.        }
  132.        else {
  133.           if (Flip IS FALSE) {
  134.              if (Rabbit->Frame < 16) Rabbit->Frame = 16;
  135.              if (Rabbit->Frame > 24) Rabbit->Frame = 16;
  136.           }
  137.           else {
  138.              if (Rabbit->Frame < 25) Rabbit->Frame = 25;
  139.              if (Rabbit->Frame > 47) Rabbit->Frame = 25;
  140.           }
  141.        }
  142.     }
  143.  
  144.     /* Get the user input */
  145.  
  146.     Query(joydata);
  147.     if (Direction != NULL) {
  148.        Rabbit->XCoord += joydata->XChange;
  149.     }
  150.  
  151.     if (joydata->YChange < 0) {
  152.        if ((Direction != NULL) OR (Flip IS TRUE)) {
  153.           Rabbit->Frame = 16;
  154.           Direction = 0;
  155.           Flip = FALSE;
  156.        }
  157.     }
  158.     else if (joydata->YChange > 0) {
  159.        if ((Direction != NULL) OR (Flip IS FALSE)) {
  160.           Rabbit->Frame = 25;
  161.           Direction = 0;
  162.           Flip = TRUE;
  163.        }
  164.     }
  165.     else if (joydata->XChange > 0) {
  166.        if (Direction IS -1) {   /* If rabbit is facing left, spin right */
  167.           Rabbit->Frame -= 8;
  168.           Direction     = 1;
  169.        }
  170.        else if (Direction IS NULL) {
  171.           Rabbit->Frame = 0;
  172.           Direction     = 1;
  173.        }
  174.     }
  175.     else if (joydata->XChange < 0) {
  176.        if (Direction IS 1) {    /* If rabbit is facing right, spin left */
  177.           Rabbit->Frame += 8;
  178.           Direction     = -1;
  179.        }
  180.        else if (Direction IS NULL) {
  181.           Rabbit->Frame = 8;
  182.           Direction     = -1;
  183.        }
  184.     }
  185.  
  186.     Wrap(Rabbit);
  187.  
  188.   } while (!(joydata->Buttons & JD_FIRE1));
  189. }
  190.  
  191. /*****************************************************************************
  192. ** Function: This function will wrap a bob to the other side of a screen if
  193. **           it leaves the bob's screen borders.
  194. **
  195. ** Synopsis: Wrap(Bob);
  196. */
  197.  
  198. void Wrap(struct Bob *bob)
  199. {
  200.   if (bob->XCoord < -bob->Width)  bob->XCoord = bob->DestBitmap->Width;
  201.   if (bob->YCoord < -bob->Height) bob->YCoord = bob->DestBitmap->Height;
  202.  
  203.   if (bob->XCoord > bob->DestBitmap->Width)  bob->XCoord = -bob->Width;
  204.   if (bob->YCoord > bob->DestBitmap->Height) bob->YCoord = -bob->Height;
  205. }
  206.  
  207.